package protocol.JTT1078_2016;

import utils.HexUtils;
import java.nio.charset.Charset;

public class JTT1078_2016Cmd {

    private byte[] mCmd;
    private int mIndex = 0;
    private final int MINIndex;
    public boolean isSubPack = false;
    public int sumPackNum = 1;
    public int currentPack = 1;

    private int mLen = 0;

    public JTT1078_2016Cmd(byte[] cmd){
        this.mCmd = cmd;

        int att = cmd[3] & 0xFF;
        att = att * 256 + (cmd[4] & 0xFF);
        mLen = att&0x1FF;//长度取高属性9位

        isSubPack =  ((att >> 13&0x01) == 0x01);//是否分包

        if (isSubPack){
            sumPackNum = cmd[13] & 0xFF;
            sumPackNum = sumPackNum * 256 + (cmd[14] & 0xFF);

            currentPack = cmd[15] & 0xFF;
            currentPack = currentPack * 256 + (cmd[16] & 0xFF);

            MINIndex = 17;
        }else{
            MINIndex = 13;
        }

        mIndex = MINIndex;
    }

    public void resetIndex(){
        mIndex = MINIndex;
    }

    public void setIndex(int mIndex){
        this.mIndex = mIndex;
    }

    public int getIndex(){
        return mIndex;
    }

    /**
     * 获取命令字
     * @return
     */
    public String readOrder(){
        return HexUtils.toHexStr(new byte[]{mCmd[6], mCmd[7]});
    }

    public void moveIndex(int len){
        mIndex += len;
    }

    public byte order(){
        return mCmd[1];
    }

    public byte suOrder(){
        return mCmd[2];
    }

    public byte[] read(int len){
        byte[] data = new byte[len];
        System.arraycopy(mCmd, mIndex,data,0,len);

        return data;
    }

    public int readWORD(){
        return readInt(2);
    }

    public int readDWORD(){
        return readInt(4);
    }


    public int readInt(int len){
        if (len > 4){
            //int类型最多占4个字节
            return -1;
        }
        int num = 0;
        for (int i= 0;i<len;i++){
            num = num << 8;
            num = num |(mCmd[mIndex +i] &0xFF);
        }
        mIndex += len;
        return num;
    }

    public String readString(int len){
        return readString(len,Charset.forName("GBK"));
    }

    /**
     * 获取字符串
     * @param len 如果为-1，则持续读到0x00判定结束
     * @param charset
     * @return
     */
    public String readString(int len, Charset charset){

        if (len < 0){
            for (int i = 0; i< mCmd.length; i++){
                if (mCmd[mIndex +i] == 0x00){
                    len = i+1;
                    break;
                }
            }
        }

        byte[] data = new byte[len];
        System.arraycopy(mCmd, mIndex,data,0,len);
        mIndex += len;
        return new String(data,charset);
    }

    public String readBCD(int len){
        byte[] data = new byte[len];
        System.arraycopy(mCmd, mIndex,data,0,len);
        mIndex += len;
        return HexUtils.toHexStr(data);
    }

    /**
     * 剩余可读内容大小
     * @return
     */
    public int leftSize(){
        return mCmd.length - 2 - mIndex;
    }

    public void addSubPack(JTT1078_2016Cmd JTT10782016Cmd){
        int size = JTT10782016Cmd.leftSize();

        byte[] temp = new byte[mCmd.length + size];
        System.arraycopy(mCmd, 0, temp,0, mCmd.length);

        int index = mCmd.length - 2+size;

        byte[] data = JTT10782016Cmd.read(size);
        System.arraycopy(data, 0, temp, mCmd.length - 2, size);

        mCmd = temp;

        byte crc = 0;
        //检验码指从消息头开始，同后一字节异或，直到校验码前一个字节，占用1个字节
        for (int i = 1; i < index; i++){
            crc ^= mCmd[i];
        }

        System.out.println(HexUtils.toHexStr(mCmd));

        mCmd[index] = crc;
        mCmd[index+1] = 0x7e;

        System.out.println(HexUtils.toHexStr(mCmd));
    }


    public JTT1078_2016Cmd copy(){
        JTT1078_2016Cmd copy = new JTT1078_2016Cmd(mCmd);
        copy.mIndex = mIndex;
        return copy;
    }

    public int len() {
        return mLen;
    }
}
